package com.budwk.app.miniapp.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.miniapp.models.WxConfig;
import com.budwk.app.miniapp.services.WxconfigService;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import org.nutz.mvc.annotation.*;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/miniapp/config")
@ApiDefinition(tag = "微信小程序-参数配置管理")
@Slf4j
public class WxconfigController {
    @Inject
    private WxconfigService wxConfigService;

    /**
     * 分页查询参数配置
     * @param bean
     * @param pageNo
     * @param pageSize
     * @param pageOrderName
     * @param pageOrderBy
     * @return
     */
    @At("/list")
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询参数配置")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = WxConfig.class
    )
    @ApiResponses(
            implementation = WxConfig.class
    )
    public Result<?> list(@Param("..") WxConfig bean,
                          @Param("pageNo") int pageNo,
                          @Param("pageSize") int pageSize,
                          @Param("pageOrderName") String pageOrderName,
                          @Param("pageOrderBy") String pageOrderBy) {
        return Result.data(wxConfigService.listPage(pageNo, pageSize, wxConfigService.querySqlBuild(bean, pageOrderName, pageOrderBy)));
    }

    /**
     * 新增参数配置
     * @param wxConfig
     * @param req
     * @return
     */
    @At("/add")
    @Ok("json")
    @POST
    @ApiOperation(name = "新增参数配置")
    @ApiFormParams(
            implementation = WxConfig.class
    )
    @ApiResponses
    @SLog(tag = "新参数配置", value = "${args[0].id}")
    @Aop(TransAop.READ_COMMITTED)
    public Result<?> create(@Param("..") WxConfig wxConfig, HttpServletRequest req) {
        wxConfig.setCreateBy(SecurityUtil.getUserId());
        wxConfigService.insert(wxConfig);
        return Result.success();
    }


    /**
     * 修改参数配置
     * @param wxConfig
     * @param req
     * @return
     */
    @At("/upadte")
    @Ok("json")
    @POST
    @ApiOperation(name = "修改参数配置")
    @ApiFormParams(
            implementation = WxConfig.class
    )
    @ApiResponses
    @SLog(tag = "修改参数配置", value = "${args[0].id}")
    @Aop(TransAop.READ_COMMITTED)
    public Result<?> update(@Param("..") WxConfig wxConfig, HttpServletRequest req) {
        wxConfig.setUpdateBy(SecurityUtil.getUserId());
        wxConfigService.updateIgnoreNull(wxConfig);
        return Result.success();
    }

    /**
     * 获取参数配置
     * @param id
     * @param req
     * @return
     */
    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取参数配置")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    public Result<?> getData(@Param("id") Long id, HttpServletRequest req) {
        Cnd cnd = Cnd.NEW();
        cnd.and("config_id", "=", id);
        WxConfig wxConfig = wxConfigService.fetch(cnd);
        if (wxConfig == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        req.setAttribute("_slog_msg", wxConfig.getId());
        return Result.data(wxConfig);
    }

    /**
     * 删除参数配置
     * @param id
     * @param req
     * @return
     */
    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除参数配置")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SLog(tag = "删除参数配置", value = "${args[0]}")
    @SaCheckLogin
    @Aop(TransAop.READ_COMMITTED)
    public Result<?> delete(@Param("id") String id, HttpServletRequest req) {
        WxConfig wxConfig = wxConfigService.fetch(id);
        if (wxConfig == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxConfigService.delete(id);
        req.setAttribute("_slog_msg", wxConfig.getId());
        return Result.success();
    }
}
